﻿using CKS2320.Common.Utils;
using Org.BouncyCastle.Asn1.Ocsp;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CKS2320.Controller
{
    public class TimerService
    {
        public System.Threading.Timer _timer;
        public void SetupDailyTask()
        {
            TimeSpan targetTime = new TimeSpan(18, 41, 0); // 2:00 AM
            TimeSpan timeToGo = CalculateTimeToNextRun(targetTime);
            _timer = new System.Threading.Timer(OnTimerElapsed, null, timeToGo, TimeSpan.FromHours(24));
        }

        public TimeSpan CalculateTimeToNextRun(TimeSpan targetTime)
        {
            DateTime now = DateTime.Now;
            DateTime todayTarget = DateTime.Today.Add(targetTime);

            if (now > todayTarget)
            {
                // If the time has already passed today, set it for tomorrow
                todayTarget = todayTarget.AddDays(1);
            }

            return todayTarget - now;
        }

        public void OnTimerElapsed(object state)
        {
            // string response = GetApiUtils.GetPostApi(null, _setting.system.webApiUrl + "api/Shape/Add").GetAwaiter().GetResult();
            string aaa = "111";
        }
    }
}
